package com.jingyuan.biz.event.listener;

import com.google.common.collect.Lists;
import com.jingyuan.biz.event.EventManager;
import com.jingyuan.biz.event.RegistEvent;
import com.jingyuan.biz.event.VoteEvent;
import com.jingyuan.biz.manager.JNode;
import com.jingyuan.biz.manager.JNodeManager;
import com.jingyuan.biz.manager.VoteProcessManager;
import com.jingyuan.common.entity.EventType;
import com.jingyuan.common.entity.HostInfo;
import com.jingyuan.common.entity.packet.ServerMessageType;
import com.jingyuan.common.entity.ServerMessage;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RegistListener extends EventListener<RegistEvent> {
    @Resource
    private JNodeManager nodeManager;

    @Resource
    private EventManager eventManager;

    @Resource
    private VoteProcessManager voteProcessManager;

    @Override
    public void actionListener(RegistEvent event) {
        System.out.println("收到上线消息");
        JNode node=event.getNode();
        node.setLastKeepLiveTime(System.currentTimeMillis());
        List<JNode> hostNodes=nodeManager.getHostNode();
        if(hostNodes.isEmpty()){
            node.setIsMaster(true);
            nodeManager.createNode(null,node);  //创建节点
            ServerMessage onlineACK=new ServerMessage();
            onlineACK.setMaster(true);
            onlineACK.setMsg("收到本机上线确认包");
            onlineACK.setServerMessageType(ServerMessageType.REGIST_SUCCESS);
            node.getChannel().writeAndFlush(onlineACK);
        }else{
            List<HostInfo> hostList= Lists.newArrayList();
            HostInfo nodeInfo=new HostInfo();
            nodeInfo.setAddress(node.getAddress());
            nodeInfo.setPort(node.getPort());
            nodeInfo.setMaster(node.getIsMaster());
            //向其他节点发送主机上线信息
            for(JNode jNode:hostNodes){
                HostInfo hostInfo =new HostInfo();
                hostInfo.setAddress(jNode.getAddress());
                hostInfo.setPort(jNode.getPort());
                hostInfo.setMaster(jNode.getIsMaster());
                hostList.add(hostInfo);
                Channel channel=jNode.getChannel();
                ServerMessage response=new ServerMessage();
                response.setHost(nodeInfo);
                response.setServerMessageType(ServerMessageType.OTHER_HOST_REGIST_SUCCESS); //上线通知消息
                response.setMsg(String.format("------------------------IP为 %s 的机器上线----------------------",node.getAddress()));
                channel.writeAndFlush(response);
            }
            nodeManager.createNode(null,node);  //创建节点
            //向刚上线的主机发送其他线上主机信息
            ServerMessage onlineACK=new ServerMessage();
            onlineACK.setOnlinehHostList(hostList);
            onlineACK.setMsg("收到本机上线确认包");
            onlineACK.setServerMessageType(ServerMessageType.REGIST_SUCCESS);
            node.getChannel().writeAndFlush(onlineACK);

            //此节点上线的时候如果正好处于选举阶段，则通知此节点参与选举
            boolean isOnVoting = voteProcessManager.getIsStartVoting().get();
            if(isOnVoting){
                VoteEvent voteEvent=new VoteEvent();
                voteEvent.setAddress(node.getAddress());
                voteEvent.setPort(node.getPort());
                voteEvent.setNode(node);
                //当前正在进行选举过程
                voteEvent.setPhase(VoteEvent.VOTE_NOW);
                eventManager.dispatch(voteEvent);
            }
        }


    }

    public RegistListener(){
        setListenEventType(EventType.ONLINE);
    }
}
